﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;

namespace SecuritySystem
{
    public partial class AddTypeForm : Form
    {
        List<ProductType> productTypeList;
        TIEntities dbContext ;
        public AddTypeForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            FillTypeCombo();
        }
        private void FillTypeCombo()
        {

            productTypeList = (from pt in dbContext.ProductTypes select pt).Distinct().ToList();
            if (productTypeList != null)
            {
                BindDataWithListBox(productTypeList);
            }
            else
            {
                productTypeList = new List<ProductType>();
            }
            
        }

        private void btnAddNewProductType_Click(object sender, EventArgs e)
        {
           string typeName  =textBoxNewProductType.Text.Trim();
            if (!string.IsNullOrWhiteSpace(typeName))
            {
                ProductType productType;

                productType = (from p in dbContext.ProductTypes  where p.Name == typeName select p).FirstOrDefault();
                if (productType ==null)
                {
                    productType = new ProductType();
                    productType.Name = typeName;
                    dbContext.AddToProductTypes(productType);
                    
                    dbContext.SaveChanges();
                  
                    productTypeList.Add(productType);

                    textBoxNewProductType.Clear();
                    BindDataWithListBox(productTypeList);


                    ProductSubType productSubType = new ProductSubType();
                    productSubType.Name = "--";
                    productType.Description = "--";
                    productSubType.ProductTypeID = productType.ID;
                    dbContext.AddToProductSubTypes(productSubType);
                    dbContext.SaveChanges();

                    ProductSize size = new ProductSize();
                    size.Name = "--";
                    size.TypeID = productType.ID;
                    dbContext.AddToProductSizes(size);
                    dbContext.SaveChanges();



                     
                 }
                else
                {
                    MessageBox.Show("Type already present in database.");
                }
                
            }
            else
            {
                MessageBox.Show("Invalid Type Name.");
            }
        }

        private void BindDataWithListBox(List<ProductType> productTypeList)
        {
           // listBoxProdcutType.Items.Clear();
            listBoxProdcutType.DataSource = null;
            listBoxProdcutType.DisplayMember = "Name";
            listBoxProdcutType.ValueMember = "ID";
            listBoxProdcutType.DataSource = productTypeList;
            listBoxProdcutType.Refresh();
        }

        private void listBoxProdcutType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

    }
}
